define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'material/file/index' + location.search,
                    add_url: 'material/file/add',
                    edit_url: 'material/file/edit',
                    del_url: 'material/file/del',
                    multi_url: 'material/file/multi',
                    import_url: 'material/file/import',
                    table: 'material_file',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                // 表单
                var form = $("form", table.$commonsearch);

                // 设置绑定课程
                $("input[name='course_id']", form).addClass("selectpage").data("source", "course/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);

                // 注册事件
                Form.events.selectpage(form);
            });           

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'desc', title: __('Desc'), operate: 'LIKE'},
                        {field: 'price', title: __('Price')},
                        {field: 'hot', title: __('Hot')},
                        {field: 'course_id', title: __('所属题库')},
                        {field: 'course.name', title: __('Course_id'),searchable: false},
                        {
                            field: 'createtime',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Update_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // $(".btn-add").data("area",["85%","85%"]);
            // $(".btn-edit").data("area",["85%","85%"]);
            // table.on("post-body.bs.table",function (){
            //     $(".btn-editone").data("area",["85%","85%"]);
            // });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 动态修改请求参数
            table.bootstrapTable('getOptions').extend.add_url = 'material/file/add/course_id/' + $('#course_id').val();
        },
        add: function () {
            var rand_x = 700;
            var rand_y = 2000;
            $("#c-hot").val(Math.round(Math.random() * (rand_y - rand_x) + rand_x));
            Controller.versionChange()
            Controller.api.bindevent();
        },
        versionChange(){
            // 监听单选按钮的 change 事件
            $('input[name="row[version]"]').change(function() {
                var selectedValue = $(this).val();
                if (selectedValue === '1') {
                    $('.m_type_1').show()
                    $('.m_type_2').hide()
                } else if (selectedValue === '2') {
                    $('.m_type_1').hide()
                    $('.m_type_2').show()
                }
            });
        },
        edit: function () {
            Controller.versionChange();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
